﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KinectedMusic.Kinect
{
    class GestureSwipeLeft : IGesture
    {
        public bool Detect(List<Entry> positionsLeftHand, List<Entry> positionsRightHand)
        {
            //return CheckForSwipe(positionsLeftHand) | CheckForSwipe(positionsRightHand);
            return CheckForSwipe(positionsRightHand);
        }

        private bool CheckForSwipe(List<Entry> hand)
        {
            //?? make one hand down the bottom??

            int start = 0;
            int errors = 0;
            float heightError = .1f;
            float minWidth = .4f;

            //check before??
            if (hand.Count <= 8)
                return false;

            //why at one??
            for (int x = 1; x < hand.Count; x++)
            {
                if (Math.Abs(hand[start].Joint.Position.Y - hand[x].Joint.Position.Y) > heightError)
                {
                    errors++;
                }
                if ((hand[start].Joint.Position.X - hand[x].Joint.Position.X) < 0)
                {
                    errors++;
                }

                if (errors >= 2)
                {
                    start = x;
                    errors = 0;
                }
                else if ((hand[start].Joint.Position.X - hand[x].Joint.Position.X) > minWidth)
                {
                    return true;
                }

            }
            return false;
        }


        public Gesture GetGesture()
        {
            return Gesture.GestureSwipeLeft;
        }
    }
}
